// pages/volunteer/awards.js
Page({
  data: {
    activeCategory: 'all',
    totalAwards: 12,
    completedAwards: 5,
    progressPercent: 42,
    awardsList: [
      {
        id: 1,
        title: '新手志愿者',
        description: '完成首次志愿活动',
        icon: '🌟',
        category: 'service',
        status: 'completed',
        current: 1,
        target: 1,
        progress: 100,
        reward: '50积分 + 新手徽章',
        completedDate: '2024-01-15'
      },
      {
        id: 2,
        title: '服务达人',
        description: '累计参与10次志愿活动',
        icon: '🏆',
        category: 'service',
        status: 'completed',
        current: 10,
        target: 10,
        progress: 100,
        reward: '200积分 + 服务达人徽章',
        completedDate: '2024-03-20'
      },
      {
        id: 3,
        title: '爱心大使',
        description: '累计参与50次志愿活动',
        icon: '💖',
        category: 'service',
        status: 'pending',
        current: 25,
        target: 50,
        progress: 50,
        reward: '500积分 + 爱心大使徽章',
        completedDate: null
      },
      {
        id: 4,
        title: '活动组织者',
        description: '成功组织3次志愿活动',
        icon: '📋',
        category: 'activity',
        status: 'pending',
        current: 1,
        target: 3,
        progress: 33,
        reward: '300积分 + 组织者徽章',
        completedDate: null
      },
      {
        id: 5,
        title: '宣传能手',
        description: '发布10篇志愿活动推文',
        icon: '📢',
        category: 'activity',
        status: 'completed',
        current: 10,
        target: 10,
        progress: 100,
        reward: '150积分 + 宣传能手徽章',
        completedDate: '2024-02-10'
      },
      {
        id: 6,
        title: '推荐达人',
        description: '成功推荐5名新志愿者',
        icon: '👥',
        category: 'activity',
        status: 'pending',
        current: 2,
        target: 5,
        progress: 40,
        reward: '250积分 + 推荐达人徽章',
        completedDate: null
      },
      {
        id: 7,
        title: '年度志愿者',
        description: '年度服务时长达到100小时',
        icon: '⭐',
        category: 'special',
        status: 'pending',
        current: 65,
        target: 100,
        progress: 65,
        reward: '1000积分 + 年度志愿者徽章',
        completedDate: null
      },
      {
        id: 8,
        title: '特殊贡献',
        description: '在特殊活动中表现突出',
        icon: '🎖️',
        category: 'special',
        status: 'completed',
        current: 1,
        target: 1,
        progress: 100,
        reward: '300积分 + 特殊贡献徽章',
        completedDate: '2024-04-05'
      },
      {
        id: 9,
        title: '连续签到',
        description: '连续签到30天',
        icon: '📅',
        category: 'service',
        status: 'completed',
        current: 30,
        target: 30,
        progress: 100,
        reward: '100积分 + 坚持徽章',
        completedDate: '2024-01-30'
      },
      {
        id: 10,
        title: '积分达人',
        description: '累计获得10000积分',
        icon: '💰',
        category: 'special',
        status: 'pending',
        current: 8500,
        target: 10000,
        progress: 85,
        reward: '500积分 + 积分达人徽章',
        completedDate: null
      },
      {
        id: 11,
        title: '团队合作',
        description: '参与5次团队志愿活动',
        icon: '🤝',
        category: 'activity',
        status: 'pending',
        current: 3,
        target: 5,
        progress: 60,
        reward: '200积分 + 团队合作徽章',
        completedDate: null
      },
      {
        id: 12,
        title: '环保先锋',
        description: '参与环保类志愿活动10次',
        icon: '🌱',
        category: 'service',
        status: 'pending',
        current: 7,
        target: 10,
        progress: 70,
        reward: '300积分 + 环保先锋徽章',
        completedDate: null
      }
    ],
    filteredAwards: []
  },

  onLoad(options) {
    console.log('奖项页面加载');
    this.loadAwardsData();
    this.filterAwards();
  },

  onShow() {
    // 页面显示时刷新数据
    this.refreshData();
  },

  // 加载奖项数据
  loadAwardsData() {
    // 从本地存储或服务器获取奖项数据
    const awardsData = wx.getStorageSync('volunteerAwardsData');
    if (awardsData) {
      this.setData({
        awardsList: awardsData.awardsList || this.data.awardsList
      });
    }
    this.calculateStats();
  },

  // 计算统计数据
  calculateStats() {
    const { awardsList } = this.data;
    const totalAwards = awardsList.length;
    const completedAwards = awardsList.filter(award => award.status === 'completed').length;
    const progressPercent = totalAwards > 0 ? Math.round((completedAwards / totalAwards) * 100) : 0;

    this.setData({
      totalAwards,
      completedAwards,
      progressPercent
    });
  },

  // 切换分类
  switchCategory(e) {
    const category = e.currentTarget.dataset.category;
    this.setData({
      activeCategory: category
    });
    this.filterAwards();
  },

  // 筛选奖项
  filterAwards() {
    const { awardsList, activeCategory } = this.data;
    let filteredAwards = awardsList;

    if (activeCategory !== 'all') {
      filteredAwards = awardsList.filter(award => award.category === activeCategory);
    }

    this.setData({
      filteredAwards
    });
  },

  // 刷新数据
  refreshData() {
    this.loadAwardsData();
    this.filterAwards();
  },



  // 分享功能
  onShareAppMessage() {
    return {
      title: '我的志愿者奖项',
      path: '/pages/volunteer/awards',
      imageUrl: '/images/share-awards.png'
    };
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: '我的志愿者奖项',
      imageUrl: '/images/share-awards.png'
    };
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.refreshData();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  }
});